﻿using System;

namespace ForgeCraft
{
    public class CmdKill : Command
    {
        public override string name { get { return "kill"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "op"; } }
        public override bool consoleUsable { get { return false; } }
        public override Permission defaultRank { get { return Permission.Guest; } }
        public CmdKill() { }

        public override void Use(Player p, string message)
        {
         
            string[] param = message.Split(' ');

            if (param.Length == 2)
            {
                short damage = 0;
                short.TryParse(param[1], out damage);

                Player target = Server.FindPlayer(param[0]);

                if (target != null)
                {
                    target.info.health -= damage;
                    target.Send(PacketWriter.MakeUpdateHealth(target), false);
                }
                else
                {
                    p.NoPlayerMessage(param[0]);
                }
            }
            else if (param.Length == 1)
            {
                short damage = 0;
                short.TryParse(message, out damage);

                if (damage == 0)
                {
                    Player target = Server.FindPlayer(message);

                    if (target != null)
                    {
                        target.info.health = 0;
                        target.Send(PacketWriter.MakeUpdateHealth(target), false);
                    }
                    else
                    {
                        p.NoPlayerMessage(message);
                    }
                }
                else
                {
                    p.info.health -= damage;
                    p.Send(PacketWriter.MakeUpdateHealth(p), false);
                }
            }
            else
            {
                p.info.health = 0;
                p.Send(PacketWriter.MakeUpdateHealth(p), false);
            }
        }

        public override void Help(Player p)
        {
            p.Message(Color.Aqua + "/kill <player> <damage> " + Color.Help + "- Causes [damage] to [player].");
        }
    }
}